# coding:utf-8
"""
    查看不同基元的变化
"""
import VRC4FPython as v4
import os
from subsector import subsector_type1
from vlogic_analyze import process_nonoverlap
from utils import filename_creator,string_xor,statistic, right_shift
from copy import copy

KEEP = 0
RIGHT_SHIFT = 1


def create_file(pArray,qArray,size,fileBase):
    """
    """
    file_names = []
    key = "houseyoung"
    for (p,q) in zip(pArray, qArray):
        fileName = fileBase+filename_creator(p, q, size)
        v4.PyVLRC4SaveKeyStreamToFile_SingleSeed(key=key,fileName=fileName,p=p,q=q,size=size)
        file_names.append(fileName)
    return file_names

def create_file_multiseed(pArray, qArray, size, fileBase):
    """
    """
    file_names = []
    key = "houseyoung"
    for (p, q) in zip(pArray, qArray):
        fileName = fileBase+filename_creator(p, q, size)
        v4.PyVLRC4SaveKeyStreamToFile_MultiSeed(pArray=[1,2],qArray=[1,2],size=100,key="houseyoung",fileName="d:/TestData/test/123.dat")
        file_names.append(fileName)
    return file_names

def get_info(r_string, window_size):
    """
        获取这个r_string的p，q信息
    """
    p_array = []
    q_array = []
    info = {}
    p = 1
    q = 1
    size = 1000
    subsection = subsector_type1(r_string,window_size)
    for coordinate in subsection:
        if coordinate[0] < coordinate[1]:
            (p, q) = process_nonoverlap(r_string[coordinate[0]:coordinate[1]+1])
            # pdb.set_trace()
            p_array.append(copy(p))
            q_array.append(copy(q))
    info["p_array"] = set(p_array)
    info["q_array"] = set(q_array)
    info["sts_info"] = statistic(p_array, q_array)
    return info

def get_rate(base,new_info):
    """
    """
    result = {}
    for p in base.keys():
        if new_info.get(p) != None:
           result[p] = float(new_info.get(p)) / base[p]
    
    return result


def analyze_stream(string):
    """
        按照一个工作流的方式来分析一个数据的变化
    """
    operations = [
        {
            "op":KEEP,
            "window_size":8
        },
        {
            "op":RIGHT_SHIFT,
            "param":1,
            "window_size":8
        },
        {
            "op":RIGHT_SHIFT,
            "param":3,
            "window_size":8
        },
        {
            "op":RIGHT_SHIFT,
            "param":7,
            "window_size":8
        },
        {
            "op":RIGHT_SHIFT,
            "param":8,
            "window_size":8
        },
        {
            "op":RIGHT_SHIFT,
            "param":9,
            "window_size":8
        },
        {
            "op":RIGHT_SHIFT,
            "param":10,
            "window_size":8
        },
    ]
    result = []
    base_p_q = {}
    for operation in operations:
        if operation["op"] == RIGHT_SHIFT:
            info = get_info(right_shift(string, operation["param"]), operation["window_size"])
            result.append(info)
            if base_p_q:
                # 如果有base的话，计算分散的速度
                change_info = get_rate(base_p_q, info["sts_info"])
                info["change_info"] = change_info

        elif operation["op"] == KEEP:
            info = get_info(string, operation["window_size"])
            base_p_q = info["sts_info"]
            result.append(info)
    return result



def print_result(result):
    """
        输出结果
    """
    for info in result:
        #  先输出文件名 
        print info["file_name"]
        #  输出相关信息
        for info_item in info["result"]:
            print info_item

def process(pArray, qArray, window_size):
    """
    """
    file_base = "D:/TestData/test/"
    files = create_file(pArray, qArray, window_size, file_base)
    string_array = []
    infos = []

    for file_name in files:
        info = {}
        r_string = open(file_name, "r").read()
        info["result"] = analyze_stream(r_string)
        info["file_name"] = file_name
        infos.append(info)
    return infos




def main():
    """
        测试
    """
    p_array = [1,2,3,4]
    q_array = [1,1,1,1]
    window_size = 8

    result = process(p_array, q_array, window_size)
    print_result(result) 



if __name__ == '__main__':
    main()
    
